﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class FrontEnd_Control_Menus_MenuTopDetail
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If Page.RouteData.Values("MenuId") <> "" Then
                hidId.Value = Page.RouteData.Values("MenuId")
            Else
                If hidId.Value = "" Then
                    hidId.Value = ConfigurationManager.AppSettings("MenuIdIndex") 'Id Menu hien thi cho trang index va active MenuId khi o Index
                End If
            End If
            BindMenu()
        End If
    End Sub
    Protected Sub BindMenu()
        Using data As New VSEntities
            'ParenId = 0 Menu cha
            'MenuPosition = 1 hien thi tren menu
            Dim Menu = (From p In data.Menus Where p.ParentId = 0 And p.MenuPosition = 1 Order By p.Sort Select p).ToList
            With rptMenu
                .DataSource = Menu
                .DataBind()
            End With
        End Using
    End Sub

    Protected Sub rptMenu_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptMenu.ItemDataBound
        Using Data As New VSEntities
            Dim row = CType(e.Item.DataItem, Menu)
            Dim isMenuId As Integer = row.MenuId
            Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)

            Dim isMenuSub = (From q In Data.Menus Where q.ParentId = isMenuId And q.MenuPosition = 1 Order By q.Sort Select q).FirstOrDefault
            If Not isMenuSub Is Nothing Then
                hplTitle.NavigateUrl = "javascript: void(0);"
                'hplTitle.ID = "jsid-header-funny-menu"
                hplTitle.CssClass = "clickshow"
                hplTitle.Text = "<Span class='label'>" & row.Title & "<span class='drop-arrow'>" & "</span>" & "</Span>"
            Else
                hplTitle.NavigateUrl = MainDomain & row.QueryString & "-" & row.MenuId & ".html"
                'hplTitle.Target = row.Style
                hplTitle.Text = "<Span class='label'>" & row.Title & "</Span>"
            End If

            'active click menu
            'Dim checkhidId = CDbl(hidId.Value)
            If isMenuId = hidId.Value Then
                hplTitle.CssClass = ""
                hplTitle.CssClass = "selected"

            End If
            'lay cac con cua menu cha theo isMenuId neu co
            Dim MenuSub = (From q In Data.Menus Where q.ParentId = isMenuId And q.MenuPosition = 1 Order By q.Sort Select q).ToList
            If MenuSub.Count > 0 Then
                Dim ltrDivMenuSub1 As Literal = CType(e.Item.FindControl("ltrDivMenuSub1"), Literal)
                ltrDivMenuSub1.Text = "<div id='jsid-header-funny-menu-items' class='popup-menu funny hide badge-popup-menu'><span class='arrow-wrap'><span class='arrow'></span></span><ul>"
                Dim rptMenuSub As Repeater = CType(e.Item.FindControl("rptMenuSub"), Repeater)
                Dim ltrDivMenuSub2 As Literal = CType(e.Item.FindControl("ltrDivMenuSub2"), Literal)
                ltrDivMenuSub2.Text = "</ul></div>"
                'rptMenuSub load cac con


                With rptMenuSub
                    .DataSource = MenuSub
                    .DataBind()
                End With
            End If
        End Using
    End Sub
    Protected Sub rptMenuSub_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        Using Data As New VSEntities
            Dim row = CType(e.Item.DataItem, Menu)
            Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
            hplTitle.NavigateUrl = MainDomain & row.QueryString & "-" & row.MenuId & ".html"
            'hplTitle.Target = row.Style
            hplTitle.Text = "<Span class='label'>" & row.Title & "</Span>"
        End Using
    End Sub
End Class
